package com.tcgroup.phone.base_core.framework.mvvm.viewmodel;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.tcgroup.phone.base_core.event.event_livedata.SingleLiveEvent;
import com.tcgroup.phone.base_core.framework.mvvm.activity.ContainerMvvmActivity;
import com.tcgroup.phone.base_core.framework.mvvm.netregister.BaseNetRegister;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/16.
 * 包名: com.tcgroup.phone.base_core.framework.mvvm.viewmodel
 * 类名: HibeautyNew-Android
 */
public class BaseViewModel<NR extends BaseNetRegister> extends AndroidViewModel implements IBaseViewModel, Consumer<Disposable> {

    protected NR mNetRegister;
    protected UIChangeLiveData mUiChangeLiveData;

    public BaseViewModel(@NonNull Application application, NR netRegister) {
        super(application);
        mNetRegister = netRegister;
    }

    public BaseViewModel(@NonNull ContainerMvvmActivity activity, NR netRegister) {
        super(activity.getApplication());
        mNetRegister = netRegister;
    }

    public UIChangeLiveData getUC() {
        if (mUiChangeLiveData == null) {
            mUiChangeLiveData = new UIChangeLiveData();
        }
        return mUiChangeLiveData;
    }

    protected SingleLiveEvent createLiveData(SingleLiveEvent liveData) {
        if (liveData == null) {
            liveData = new SingleLiveEvent();
        }
        return liveData;
    }

    public final class UIChangeLiveData extends SingleLiveEvent {
        private SingleLiveEvent<Boolean> showInitLoadViewEvent;
        private SingleLiveEvent<Boolean> showCurrentEvent;
        private SingleLiveEvent<Boolean> showNoDataViewEvent;
        private SingleLiveEvent<Boolean> showNetWorkErrViewEvent;
        private SingleLiveEvent<Map<String, Object>> startActivityEvent;
        private SingleLiveEvent<Void> finishActivityEvent;
        private SingleLiveEvent<Void> onBackPressedEvent;
        private SingleLiveEvent<Boolean> onOpenDrawerLayoutEvent;

        public SingleLiveEvent<Boolean> getShowInitLoadViewEvent() {
            return showInitLoadViewEvent = createLiveData(showInitLoadViewEvent);
        }
        public SingleLiveEvent<Boolean> getShowCurrentEventEvent() {
            return showCurrentEvent = createLiveData(showCurrentEvent);
        }
        public SingleLiveEvent<Boolean> getShowNoDataViewEvent() {
            return showNoDataViewEvent = createLiveData(showNoDataViewEvent);
        }
        public SingleLiveEvent<Boolean> getShowNetWorkErrViewEvent() {
            return showNetWorkErrViewEvent = createLiveData(showNetWorkErrViewEvent);
        }
        public SingleLiveEvent<Map<String, Object>> getStartActivityEvent() {
            return startActivityEvent = createLiveData(startActivityEvent);
        }
        public SingleLiveEvent<Void> getFinishActivityEvent() {
            return finishActivityEvent = createLiveData(finishActivityEvent);
        }
        public SingleLiveEvent<Void> getOnBackPressedEvent() {
            return onBackPressedEvent = createLiveData(onBackPressedEvent);
        }
        public SingleLiveEvent<Boolean> getOnOpenDrawerLayoutEvent(){
            return onOpenDrawerLayoutEvent = createLiveData(onOpenDrawerLayoutEvent);
        }
    }

    public void postShowCurrentEvent(boolean show) {
        if (mUiChangeLiveData != null) {
            mUiChangeLiveData.getShowCurrentEventEvent().postValue(show);
        }
    }

    public void postShowInitLoadViewEvent(boolean show) {
        if (mUiChangeLiveData != null) {
            mUiChangeLiveData.getShowInitLoadViewEvent().postValue(show);
        }
    }

    public void postShowNoDataViewEvent(boolean show) {
        if (mUiChangeLiveData != null) {
            mUiChangeLiveData.getShowNoDataViewEvent().postValue(show);
        }
    }

    public void postShowNetWorkErrViewEvent(boolean show) {
        if (mUiChangeLiveData != null) {
            mUiChangeLiveData.getShowNetWorkErrViewEvent().postValue(show);
        }
    }

    public void postOpenDrawerLayoutViewEvent(boolean show){
        if (mUiChangeLiveData != null){
            mUiChangeLiveData.getOnOpenDrawerLayoutEvent().postValue(show);
        }
    }

    public void postStartActivityEvent(Class<?> clz, Bundle bundle) {
        Map<String, Object> params = new HashMap<>();
        params.put(ParameterField.CLASS, clz);
        if (bundle != null) {
            params.put(ParameterField.BUNDLE, bundle);
        }
        mUiChangeLiveData.getStartActivityEvent().postValue(params);
    }


    public void postFinishActivityEvent() {
        mUiChangeLiveData.finishActivityEvent.call();
    }

    public void postOnBackPressedEvent() {
        mUiChangeLiveData.onBackPressedEvent.call();
    }

    public void initViewByReFresh(){

    }

    @Override
    public void onAny(LifecycleOwner owner, Lifecycle.Event event) {

    }

    @Override
    public void create() {

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void accept(Disposable disposable) {
        if(mNetRegister != null){
            mNetRegister.addSubscribe(disposable);
        }
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (mNetRegister != null) {
            mNetRegister.onCleared();
        }
    }

    public static final class ParameterField {
        public static String CLASS = "CLASS";
        public static String CANONICAL_NAME = "CANONICAL_NAME";
        public static String BUNDLE = "BUNDLE";
    }



}
